#include "../include/Dictionary.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

using std::cout;

namespace wd{

Dictionary* Dictionary::_dicttionary = nullptr;

Dictionary* Dictionary::getInstance(){
    if(_dicttionary == nullptr){
        _dicttionary = new Dictionary();
    }
    return _dicttionary;
}

void Dictionary::destory(){
    if(_dicttionary != nullptr){
        delete _dicttionary; 
        _dicttionary = nullptr;
    }
}

void Dictionary::init(const string &dicpath, const string &indexpath){
    
    std::ifstream dic_ifs(dicpath);
    if(!dic_ifs.good()){
        cout << "erro: open dic" << std::endl;
        return;
    }
    string line;
    while(getline(dic_ifs, line)){
        std::istringstream iss(line);
        string key;
        int value;
        iss >> key >>value;
        std::pair<string, int> newWord(key,value);

        _dict.push_back(newWord);

    }


    std::ifstream index_ifs(indexpath);
    if(!index_ifs.good()){
        cout << "erro: open index" << std::endl;
        return;
    }
    int number;
    string indexWord;
    while(getline(index_ifs, line)){
        std::istringstream iss(line);    
        iss >> indexWord;
        _indexTable[indexWord];
        while(iss >> number){
            /* cout << number << std::endl; */
            _indexTable[indexWord].insert(number);
        }
    }





}

std::vector<std::pair<string, int>>& Dictionary::getDict(){
    return _dict;
}

std::map<string, std::set<int>>& Dictionary::getIndexTable(){
    /* for(auto& elem: _indexTable){ */
    /*     cout << elem.first << " "; */
    /* } */
    /* cout <<std::endl; */
    return _indexTable;
}



}
